package miniapp

import (
	"wxsdk/miniapp/core"
	//"github.com/astaxie/beego"
)

// 1 获取小程序模板库标题列表.
func TemplateLibList(authorizerAccessToken string, offset, count int) (list string, err error) {
	request := struct {
		Offset int `json:"offset"`
		Count  int `json:"count"`
	}{
		Offset: offset,
		Count: count,
	}
	var result struct {
		core.Error
	}
	incompleteURL := "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token="
	serverUrl := incompleteURL + authorizerAccessToken
	client := core.GetPostJSONClient()
	var data []byte
	if data, err = client.Excute("POST", serverUrl, request, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	list = string(data)
	return
}

// 2 获取模板库某个模板标题下关键词库.
func TemplateLibGet(authorizerAccessToken string, id string) (kwlib string, err error) {
	request := struct {
	    Id string `json:"id"`
	}{
		Id: id,
	}
	var result struct {
		core.Error
	}
	incompleteURL := "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token="
	serverUrl := incompleteURL + authorizerAccessToken
	client := core.GetPostJSONClient()
	var data []byte
	if data, err = client.Excute("POST", serverUrl, request, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	kwlib = string(data)
	return
}

// 3 组合模板并添加至帐号下的个人模板库.
func TemplateAdd(authorizerAccessToken string,id string,keywordIdList []int) (templateId string, err error) {
	request := struct {
	    Id            string `json:"id"`
		KeywordIdList []int  `json:"keyword_id_list"`
	}{
		Id: id,
		KeywordIdList: keywordIdList,
	}
	var result struct {
		core.Error
		TemplateId string `json:"template_id"`  //
	}
	incompleteURL := "https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token="
	serverUrl := incompleteURL + authorizerAccessToken
	client := core.GetPostJSONClient()
	if _, err = client.Excute("POST", serverUrl, request, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	templateId = result.TemplateId
	return
}

type TempInner struct {
	TemplateId string `json:"template_id"`
	Title      string `json:"title"`
	Content    string `json:"content"`
	Example    string `json:"example"`
}

type TemplateGetResult struct {
	Lists []TempInner `json:"list"`
}
// 4 获取帐号下已存在的模板列表.
func TemplateList(authorizerAccessToken string, offset, count int) (rsp *TemplateGetResult, err error) {
	request := struct {
		Offset int `json:"offset"`
		Count  int `json:"count"`
	}{
		Offset: offset,
		Count: count,
	}
	var result struct {
		core.Error
		TemplateGetResult
	}
	incompleteURL := "https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token="
	serverUrl := incompleteURL + authorizerAccessToken
	client := core.GetPostJSONClient()
	if _, err = client.Excute("POST", serverUrl, request, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	rsp = &result.TemplateGetResult
	return
}

// 5 删除帐号下的某个模板
func TemplateDel(authorizerAccessToken string, templateId string) (err error) {
	request := struct {
		TemplateId string `json:"template_id"`
	}{
		TemplateId: templateId,
	}
	var result core.Error
	incompleteURL := "https://api.weixin.qq.com/cgi-bin/wxopen/template/del?access_token="
	serverUrl := incompleteURL + authorizerAccessToken
	client := core.GetPostJSONClient()
	if _, err = client.Excute("POST", serverUrl, request, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result
		return
	}
	return
}
